package com.app.signing.data

import android.text.TextUtils
import com.app.signing.app.App
import com.app.signing.data.bean.SeatDataDb
import com.app.signing.data.bean.SeatDataDb_
import com.app.signing.data.bean.SigningData
import com.app.signing.data.bean.UserData
import com.app.signing.data.bean.UserData_
import io.objectbox.Box
import io.objectbox.kotlin.boxFor

class DbManager private constructor() {

    private val boxSigningData: Box<SigningData> = App.app.boxStore.boxFor()
    private val boxUserData: Box<UserData> = App.app.boxStore.boxFor()
    private val boxSeatData: Box<SeatDataDb> = App.app.boxStore.boxFor()

    companion object {
        val INSTANCE: DbManager by lazy {
            DbManager()
        }
    }

    /**
     * 创建一条签到记录，包含开始时间
     */
    fun createSigning(): Long {
        val userId = SpManager.INSTANCE.readLoginUserId()
        if (TextUtils.isEmpty(userId)) {
            throw IllegalArgumentException("read login user id is empty")
        }
        val data = findUser(userId)
        data?.let {
            val signingData = SigningData(
                    userId = userId, userName = it.userName, userFaculty = it.userFaculty,
                    userTeam = it.userTeam,
                    studyStartTime = System.currentTimeMillis(), studyEndTime = 0L, studyMin = 0
            )
            return boxSigningData.put(signingData)
        }
        return 0
    }

    /**
     * 结束一次签到
     */
    fun finishSigning(id: Long) {
        val signingData = boxSigningData.get(id)
        signingData?.let {
            val endTime = System.currentTimeMillis()
            val startTime = it.studyStartTime
            val studyHour = (endTime - startTime) / (1000 * 60)
            it.studyEndTime = endTime
            it.studyMin = studyHour.toInt()
            boxSigningData.put(it)
        }
    }

    /**
     * 签到记录查询
     */
    fun loadSigning(id: Long): SigningData? {
        return boxSigningData.get(id)
    }

    /**
     * 查询所有的签到记录
     */
    fun loadSigningData(): List<SigningData> {
        return boxSigningData.query().build().find()
    }

    /**
     * 注册用户
     */
    fun registerUser(
        userId: String,
        userName: String,
        userFaculty: String,
        userTeam: String,
        password: String
    ) {
        val data = UserData(
                userId = userId, userName = userName, userFaculty = userFaculty,
                userTeam = userTeam, password = password
        )
        boxUserData.put(data)
    }

    /**
     * 查找User
     */
    fun findUser(userId: String): UserData? {
        val query = boxUserData.query().equal(UserData_.userId, userId).build()
        val list = query.find()
        return if (list.isEmpty()) {
            null
        } else {
            list[0]
        }
    }

    /**
     * 保存座位记录
     */
    fun saveSeat(seatDataDb: SeatDataDb) {
        boxSeatData.put(seatDataDb)
    }

    /**
     * 结束座位记录
     */
    fun finishSeat(signingId: Long) {
        val data = boxSeatData.query().equal(SeatDataDb_.signingId, signingId).build().find()
        data.forEach {
            it.doing = false
            boxSeatData.put(it)
        }
    }

    fun findSeat(signingId: Long): SeatDataDb? {
        val data = boxSeatData.query().equal(SeatDataDb_.signingId, signingId).build().find()
        return if (data.isEmpty()) {
            null
        } else {
            data[0]
        }
    }
}
